/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   ManagePlanServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.industrymanage    </p>
 * 
 * <p>最后修改 : $: 2017年8月17日-下午2:10:51 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.industrymanage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.industrymanage.ManagePlanService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * ManagePlanServiceImpl
 * 
 * 2017年8月17日 下午2:10:51
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class ManagePlanServiceImpl implements ManagePlanService {
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	
	@Override
	public Map<String, Object> getManagePlanMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取业务类型文件
		List<Attachment> zcList = systemManageService.getAllCheckAttachments("yhglfa_trzzc");
		List<Attachment> gzList = systemManageService.getAllCheckAttachments("yhglfa_lwjggz");
		List<Attachment> zlList = systemManageService.getAllCheckAttachments("yhglfa_sdaqyhzl");
		List<Attachment> lyList = systemManageService.getAllCheckAttachments("yhglfa_lmclxhly");
		
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearPart;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		total += zcList.size() >0 ? yearFull : 0;
		total += gzList.size() >0 ? yearFull : 0;
		total += zlList.size() >0 ? yearFull : 0;
		total += lyList.size() >0 ? yearFull : 0;
		
		total = total > yearPart ? yearPart : total;
			
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		map.put("percent", percentage);
		map.put("years", years);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
